package com.too;


import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {

        //集合只能存入引用数据类型
        //  test0();
        //  test1();
        //  test2();
        int result = sum(1, 2, 3, 4, 5);

        System.out.println(result);
    }

    private static void test0() {
        TreeSet<Integer> treeSet = new TreeSet<>();

        //将1包装成为integer对象  自动装箱。
        treeSet.add(1);
        treeSet.add(2);
        treeSet.add(5);
        treeSet.add(3);
        treeSet.add(4);


        System.out.println(treeSet);

        System.out.println("----------------迭代器------------------");
        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            Integer next = (Integer) iterator.next();
            System.out.println(next);
        }
    }


    public static void test1() {
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(new Student("张三", 18));
        treeSet.add(new Student("赵六", 21));
        treeSet.add(new Student("王五", 19));
        treeSet.add(new Student("李四", 20));

        for (Student student : treeSet)
            System.out.println(student.getName()+"-----"+student.getAge());
    }


    public static void test2() {
//        Comparator<Student> comparator = new Comparator<>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o2.getAge() - o1.getAge();
//            }
//        };

        Comparator<Student> comparator =(o1,o2)->o1.getAge()-o2.getAge();

        TreeSet<Student> treeSet1 = new TreeSet<>(comparator);
        treeSet1.add(new Student("张三", 18));
        treeSet1.add(new Student("赵六", 21));
        treeSet1.add(new Student("王五", 19));
        treeSet1.add(new Student("李四", 20));

        for (Student student : treeSet1){
            System.out.println(student.getName()+"-----"+student.getAge());
        }
    }


    //可变参数
    public static int sum(int...a){
        int total = 0;
        for (int i = 0; i < a.length; i++) {
            total  += a[i];
        }
        return total;
    }



}

















